package com.dailyblue.java.spring.boot.redis.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/string")
@Slf4j
public class StringRedisController {
    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @GetMapping("/a")
    public String a() {
        log.info("开始向redis中存放数据");
        ValueOperations value = redisTemplate.opsForValue();
        value.set("A", 1);
        value.set("B", "aaa");
        value.set("C", "nihao");
        value.set("C", "hello");
        value.set("D", "ddd", 10, TimeUnit.SECONDS);
        value.set("E", "eee", 10, TimeUnit.MINUTES);
        value.setIfAbsent("B", "bbb");
        value.setIfAbsent("F", "fff");
        return "a";
    }

    @GetMapping("/b")
    public String b() {
        log.info("开始获取redis中存放的数据");
        ValueOperations<String, String> value = redisTemplate.opsForValue();
        String v1 = value.get("A");
        String v2 = value.get("B");
        String v3 = value.get("C");
        String v4 = value.get("D");
        String v5 = value.get("E");
        String v6 = value.get("F");
        log.info("获取的数据分别是：{},{},{},{},{},{}", v1, v2, v3, v4, v5, v6);
        return "b";
    }

    @GetMapping("/c")
    public String c() {
        log.info("开始处理其他redis中的情况");
        ValueOperations value = redisTemplate.opsForValue();
        //value.increment("A", 3);
        value.append("B", "caa");
        value.set("F",null,1,TimeUnit.MILLISECONDS);
        return "c";
    }
}
